package com.music.controller;

import com.github.pagehelper.PageInfo;
import com.music.pojo.Playlist;
import com.music.pojo.UserInfo;
import com.music.service.PlayListService;
import com.music.utils.PlayListInfo;
import com.music.utils.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("playlist")
public class PlayListController {
    @Autowired
    private PlayListService playListService;

    @RequestMapping("search")
    @ResponseBody
    public PageInfo<Playlist> songPageInfo(String search, Integer pageNum) throws UnsupportedEncodingException {
        String _search = URLDecoder.decode(search,"UTF-8");
        return playListService.findPlaylistBySearch(_search,pageNum);
    }

    @RequestMapping("find")
    @ResponseBody
    public ResultInfo indexFind(Integer pageNum,Integer pageSize){
        PageInfo<Playlist> pageInfo = playListService.find(pageNum, pageSize);
        ResultInfo resultInfo = new ResultInfo(true);
        resultInfo.setData(pageInfo);
        return resultInfo;
    }

    @RequestMapping("findByMid")
    @ResponseBody
    public ResultInfo findByMid(Integer pageNum,Integer pageSize,Integer mid){
        PageInfo<Playlist> pageInfo = playListService.findByMid(pageNum, pageSize,mid);
        ResultInfo resultInfo = new ResultInfo(true);
        resultInfo.setData(pageInfo);
        return resultInfo;
    }

    @RequestMapping("findByUid")
    @ResponseBody
    public ResultInfo findByUid(HttpSession session){
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        ResultInfo resultInfo;
        if (userInfo!=null){
            List<Playlist> list = playListService.findByUid(userInfo.getUid());
            resultInfo = new ResultInfo(true);
            resultInfo.setData(list);
        }else {
            resultInfo = new ResultInfo(false);
        }
        return resultInfo;
    }

    //取消收藏
    @RequestMapping("unFavorite")
    @ResponseBody
    public ResultInfo unFavorite(Integer mid,HttpSession session){
        ResultInfo resultInfo = new ResultInfo(true);
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        playListService.removeFavorite(mid,userInfo.getUid());
        return resultInfo;
    }

    //收藏歌曲
    @RequestMapping("addFavorite")
    @ResponseBody
    public ResultInfo addFavorite(Integer mid,Integer plid){
        ResultInfo resultInfo = new ResultInfo(true);
        playListService.addFavorite(mid,plid);
        return resultInfo;
    }

    //创建新歌单并收藏歌曲
    @RequestMapping("addFavoriteToNew")
    @ResponseBody
    public ResultInfo addFavoriteToNew(Integer mid,String plname,HttpSession session){
        ResultInfo resultInfo = new ResultInfo(true);
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        playListService.addFavoriteToNew(mid,plname,userInfo.getUid());
        return resultInfo;
    }

    //根据类别名查找歌单按订阅数降序
    @RequestMapping("findByCa")
    @ResponseBody
    public PageInfo<Playlist> findPlayListByCa(@RequestParam(defaultValue = "1",required = false) String category, Integer pageNum){
        return playListService.findPlaylistByCategory(category,pageNum);
    }

    @RequestMapping("playListDetail")
    @ResponseBody
    public ResultInfo findPlayListDetail(Integer plid,HttpSession session){
        ResultInfo resultInfo = new ResultInfo(false);
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        Playlist playList;
        if (userInfo==null){
            playList = playListService.findDetailByPlid(plid,null);
            resultInfo.setFlag(true);
        }else {
            playList = playListService.findDetailByPlid(plid,userInfo.getUid());
            if (!Objects.equals(playList.getUserInfo().getUid(), userInfo.getUid())) {
                resultInfo = new ResultInfo(true);
            }
        }
        resultInfo.setData(playList);
        return resultInfo;
    }

    @RequestMapping("addFavoritePlayList")
    @ResponseBody
    public ResultInfo addFavoritePlayList(Integer plid,HttpSession session){
        ResultInfo resultInfo = new ResultInfo(true);
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        if (userInfo==null){
            return new ResultInfo(false);
        }else {
            playListService.addFavoritePlayList(plid,userInfo.getUid());
            return resultInfo;
        }
    }

    //用户移除收藏的歌单
    @RequestMapping("removeFavoritePlayList")
    @ResponseBody
    public ResultInfo removeFavoritePlayList(Integer plid,HttpSession session){
        ResultInfo resultInfo = new ResultInfo(true);
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        playListService.removeFavoritePlayList(plid,userInfo.getUid());
        return resultInfo;
    }

    //用户删除自己的歌单
    @RequestMapping("removePlayList")
    @ResponseBody
    public ResultInfo removePlayList(Integer plid){
        playListService.removePlayList(plid);
        ResultInfo resultInfo = new ResultInfo(true);
        return resultInfo;
    }

    //新增或修改用户的歌单
    @PostMapping("addOrUpdatePlayList")
    @ResponseBody
    public ResultInfo addOrUpdatePlayList(MultipartHttpServletRequest request,HttpSession session) throws IOException {
        String plname = request.getParameter("plname");
        String checkList = request.getParameter("checkList");
        String desc = request.getParameter("desc");
        MultipartFile file = request.getFile("file");
        PlayListInfo playListInfo = new PlayListInfo();
        String _plid = request.getParameter("plid");
        if (!_plid.equals("null")){
            Integer plid = Integer.valueOf(_plid);
            playListInfo.setPlid(plid);
        }
        playListInfo.setDesc(desc);
        playListInfo.setPlname(plname);
        if (!checkList.equals("")){
            List<String> cates = Arrays.asList(checkList.split(","));
            playListInfo.setCates(cates);
        }
        if (file!=null){
            String fileName = "";
            String originalFilename = file.getOriginalFilename();
            String extendName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
            fileName = uuid + "_" + originalFilename;
            ServletContext context = request.getServletContext();
            String basePath = context.getRealPath("/img");
            String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            File newfile = new File(basePath + "/" + datePath);
            playListInfo.setPicUrl("/img" + "/" + datePath+"/"+fileName);
            if (!newfile.exists()) {
                newfile.mkdirs();
            }
            file.transferTo(new File(newfile, fileName));
        }

        ResultInfo resultInfo = new ResultInfo(true);
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        playListService.addOrUpdatePlayList(playListInfo,userInfo.getUid());
        System.out.println(playListInfo);
        return resultInfo;
    }
}
